package com.hzit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hzit.dto.StudentDTO;
import com.hzit.entity.PageResult;
import com.hzit.entity.Student;
import com.hzit.entity.StudentExample;
import com.hzit.mapper.StudentMapper;
import com.hzit.service.StudentService;
import com.hzit.util.Conver;
import com.hzit.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StudentServiceImpl implements StudentService {
	@Autowired
	private StudentMapper studentMapper;
	@Autowired
	private Conver conver;

	//1. 查询学生列表
	@Override
	public List<Student> findAll() {
		return studentMapper.selectByExample(null);
	}

	//2. 分页查询
	@Override
	public PageResult<StudentDTO> findByPage(int page, int pageSize) {
		//2.1 开始分页
		PageHelper.startPage(page,pageSize);
		//2.2 开始查询所有记录
		StudentExample example = new StudentExample();
		List<Student> students = studentMapper.selectByExample(example);
		//2.3 将上面的集合转换为Page对象
		Page<Student> studentPage = (Page<Student>) students;
		//2.4 得到当前页的记录集合
		List<Student> result = studentPage.getResult();
		//2.5 将上面的集合转换为List<StudentDTO>集合
		List<StudentDTO> dtos = conver.conver2DTO(result);
		//2.6 返回PageResult对象
		return new PageResult<>(dtos,studentPage.getTotal());
	}

	//3. 条件查询带分页
	@Override
	public PageResult<StudentDTO> search(int page, int pageSize, StudentVo vo) {
		//3.1 开始分页
		PageHelper.startPage(page,pageSize);
		//3.2 开始查询所有记录
		StudentExample example = new StudentExample();
		//3.2.1 获取查询实例的条件对象
		StudentExample.Criteria criteria = example.createCriteria();
		//3.2.2 向criteria中存放查询条件
		if(vo != null){
			// ① 学生姓名的查询
			if(StrUtil.isNotBlank(vo.getSname())){
				criteria.andSnameLike("%" + vo.getSname() + "%");
			}
			// ② 学生住址的查询
			if(StrUtil.isNotBlank(vo.getAddr())){
				criteria.andAddrLike("%" + vo.getAddr() + "%");
			}
			// ③ 学生所在班级的查询
			if(vo.getCid() != null && vo.getCid() != 0){
				criteria.andCidEqualTo(vo.getCid());
			}
		}
		List<Student> students = studentMapper.selectByExample(example);
		//3.3 将上面的集合转换为Page对象
		Page<Student> studentPage = (Page<Student>) students;
		//3.4 得到当前页的记录集合
		List<Student> result = studentPage.getResult();
		//3.5 将上面的集合转换为List<StudentDTO>集合
		List<StudentDTO> dtos = conver.conver2DTO(result);
		//3.6 返回PageResult对象
		return new PageResult<>(dtos,studentPage.getTotal());

	}

	//4. 添加学生
	@Override
	public void add(Student student) {
		studentMapper.insert(student);
	}

	//5. 修改学生
	@Override
	public void update(Student student) {
		studentMapper.updateByPrimaryKey(student);
	}

	//6. 删除学生 (版本一)
	@Override
	public void delete(String[] sids) {
		//6.1 循环一个删除一个
		// for (String sid : sids) {
		// 	studentMapper.deleteByPrimaryKey(new Integer(sid));
		// }
		//6.2 可以使用example实例删除
		StudentExample example = new StudentExample();
		StudentExample.Criteria criteria = example.createCriteria();
		List<Integer> collect = Arrays.stream(sids).map(m -> Integer.parseInt(m)).collect(Collectors.toList());
		criteria.andSidIn(collect);
		studentMapper.deleteByExample(example);
	}

	//6. 删除学生 (版本二)
	@Override
	public void delete2(Integer[] sids) {
		StudentExample example = new StudentExample();
		StudentExample.Criteria criteria = example.createCriteria();
		List<Integer> sidsList = Arrays.asList(sids);
		criteria.andSidIn(sidsList);
		studentMapper.deleteByExample(example);
	}
}
